

clear 
set scheme lean2, permanent
global pm = char(177)
set more off

gl date = c(current_date)
if c(os) == "MacOSX" gl user "/Users/`c(username)'"
else if c(os) == "Windows" gl user "C:\Users\\`c(username)'"
else if c(os) == "Unix" gl user "/usr/`c(username)'"
di "$user

set graphics on
 
global path_data_processed "\data\processed"
global path_output "\data\output"

clear all
set more off

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"
 
loc covars "female age atleast_bachelor log_inc republican democrat midwest south west  prior"

keep if posterior!=.



			   
			   

/* Balance tables */


    preserve

    clear all
    eststo clear
    estimates drop _all

    set obs 10
    qui gen x = 1
    qui gen y = 1

    forval i = 1/4 {

        qui eststo col`i': reg x y

    }

    restore

/* Statistics */

    loc tabletitle "Randomization check for experiment `expid'"
    loc rowstats ""
    loc rowlabels ""
    loc colnames " "Recession: High" "Recession: Low" "P-value(High - Low)" "Observations" "

    loc varlength: list sizeof covars
    loc varindex = 1

    mat def P1 = J(`varlength', 1, .)
    mat def P2 = J(`varlength', 1, .)
    mat def P3 = J(`varlength', 1, .)

    foreach var in `covars' {


        cap noi {
            qui sum `var' if treatment == 1 
            estadd loc `var'_mean = string(r(mean), "%9.2f"): col1
        }

        cap noi {
            qui sum `var' if treatment == 0
            estadd loc `var'_mean = string(r(mean), "%9.2f"): col2
        }



        cap noi {
            qui reg `var' treatment, vce(r)
            qui test treatment = 0
            estadd loc `var'_mean = string(r(p), "%9.3f"): col3
            mat def P3[`varindex', 1] = r(p)
        }
		
				
				 cap noi {
            qui sum `var' 
            estadd loc `var'_mean =  string(r(N), "%9.0f"): col4
        }

        loc rowstats "`rowstats' `var'_mean `var'_sd"
        loc rowlabels "`rowlabels' `"`: var la `var''"' " " "

        loc ++varindex

    }

    esttab * using "balance.tex", replace cells(none) booktabs nonotes nonum compress alignment(c) nogap noobs nobaselevels label mtitle(`colnames') stats(`rowstats', labels(`rowlabels'))
    eststo clear





